<template>
  <div>
    <el-button @click="isShow=true" type="primary" plain>添加学生</el-button>
    <el-table :data="studentList" border style="width: 100%">
      <el-table-column prop="id" label="编号" width="150"></el-table-column>
      <el-table-column prop="name" label="姓名" width="150"></el-table-column>
      <el-table-column prop="fraction" label="成绩" width="150"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="delData(scope.$index)" type="text" size="small">删除</el-button>
          <el-button type="text" size="small" @click="getUpdateData(scope.$index)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- ----------------------添加表单-------------------------- -->
    <el-dialog title="添加学生信息" :visible.sync="isShow" width="30%">
      <el-form>
        <el-form-item label="编号">
          <el-input v-model="newStudent.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="newStudent.name"></el-input>
        </el-form-item>
        <el-form-item label="成绩">
          <el-input v-model="newStudent.fraction"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="postData">确 定</el-button>
      </span>
    </el-dialog>
    <!-- ------------------------------修改学生信息-------------------------- -->
    <el-dialog :data="upStudent" title="修改学生信息" :visible.sync="isShowUp" width="30%">
      <el-form>
        <!-- <template slot-scope="scope"> -->
        <el-form-item label="编号">
          <el-input :disabled="true" v-model="upStudent.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="upStudent.name"></el-input>
        </el-form-item>
        <el-form-item label="成绩">
          <el-input v-model="upStudent.fraction"></el-input>
        </el-form-item>
        <!-- </template> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="upData(upStudent.id,upStudent)">确 定</el-button>
        <el-button @click="isShowUp = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      // 学生列表
      studentList: [],
      // 添加学生信息
      newStudent: {},
      // 添加展示页面
      isShow: false,
      // 修改学生信息
      upStudent: {},
      // 修改展示页面
      isShowUp: false,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      axios
        .get("http://127.0.0.1:7001/student")
        .then((res) => {
          this.studentList = res.data;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    postData() {
      axios
        .post("http://127.0.0.1:7001/student", { newStudent: this.newStudent })
        .then(() => {
          this.getData();
        })
        .catch((err) => {
          console.error(err);
        });
    },
    delData(index) {
      axios
        .delete(`http://127.0.0.1:7001/student/${index}`)
        .then(() => {
          this.getData();
        })
        .catch((err) => {
          console.error(err);
        });
    },
    upData(id) {
      axios
        .put(`http://127.0.0.1:7001/student/${id}`, { upStudent: this.upStudent })
        .then(() => {
          this.getData();
          this.isShowUp=false;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getUpdateData(index) {
      this.isShowUp = true;
      for (let i = 0; i < this.studentList.length; i++) {
        if (i == index) {
          this.upStudent = this.copy(this.studentList[i]);
        }
      }
    },
    copy(obj) {
      let objs = {};
      for (let i in obj) {
        objs[i] = obj[i];
      }
      return objs;
    },
  },
};
</script>